#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

void mysign(int sign)
{
    cout << "sign: " << sign << " pid: " << getpid() << endl;
}

int main()
{
    // sighandler_t fun = signal(SIGINT, mysign);
    // cout << (void*)fun << endl;
    // //signal(SIGSEGV, mysign);
    // while(true)
    // {
    //     cout << "pid: " << getpid() << " 执行中..." << endl;
    //     int a = 10;
    //     a /= 0;
    //     sleep(1);
    // }

    pid_t id = fork();
    if(id == 0)
    {
        // child
        cout << "child pid: " << getpid() << endl;
        int a = 100;
        a /= 0;
        exit(0);
    }
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret == -1)
    {
        exit(1);
    }
    cout << "wait child pid: " << ret << " signal: " << (status & 0x7f) \
        << " core dump: " << ((status >> 7) & 0x1) << endl;

    return 0;
}